package cn.javabb.core.plugin.thymeleaf;

import cn.javabb.core.plugin.thymeleaf.attribute.NavTagProcessor;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.dialect.IExpressionObjectDialect;
import org.thymeleaf.expression.IExpressionObjectFactory;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;
import org.thymeleaf.standard.processor.StandardXmlNsTagProcessor;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.HashSet;
import java.util.Set;

/**
 * 定义Dialect
 */
public class BbsDialect  extends AbstractProcessorDialect implements IExpressionObjectDialect {

    private static final String NAME = "BbsDialect"; //定义方言名称
    private static final String PREFIX = "bb";
    private IExpressionObjectFactory expressionObjectFactory = null;

    public BbsDialect() {
        //  我们将设置此方言与“方言处理器”优先级相同
        //  标准方言, 以便处理器执行交错。
        super(NAME, PREFIX, StandardDialect.PROCESSOR_PRECEDENCE);
    }

    @Override
    public Set<IProcessor> getProcessors(final String dialectPrefix) {
        /*
         * 注册processors
         */
        Set<IProcessor> processors =new HashSet<IProcessor>();
        processors.add(new NavTagProcessor(dialectPrefix));
        processors.add(new StandardXmlNsTagProcessor(TemplateMode.HTML, dialectPrefix));
        return processors;
    }

    @Override
    public IExpressionObjectFactory getExpressionObjectFactory() {

        if (this.expressionObjectFactory == null) {
            this.expressionObjectFactory = new BbsExpressionObjectFactory();
        }
        return this.expressionObjectFactory;
    }
}
